<?php
require "Db\DbHelper.php";
require "Tools\WebResult.php";

use Tools\WebResult;
use Db\DbHelper;

$book_id = $_REQUEST['book_id'];
$book_category = $_REQUEST['book_category'];
$book_order = $_REQUEST['book_order'];

$book_query = explode(" ", $_REQUEST['book_query']);

if (count($book_query) == 1 && !empty($book_query[0])) {
    $book_1 = $book_query[0];
} else if (count($book_query) == 2) {
    $book_1 = $book_query[0];
    $book_2 = $book_query[1];
}

$db = DBHelper::createPDO();

// 如果按照id去查询，忽略其它所有查询条件
if (isset($book_id)) {
    $stmt = $db->prepare('select * from book where id = ?');
    $stmt->bindValue(1, $book_id);
} else {

    if ((isset($book_1) || isset($book_2)) || isset($book_order) || isset($book_category)) {
        $sql = 'select * from book ';

        $flag = false;

        // 名字模糊
        if (isset($book_1) && isset($book_2)) {
            $sql .= ' where (binary name like ? or binary author like ? or binary name like ? or binary author like ?) ';
            $flag = true;
        } else if (isset($book_1)) {
            $sql .= ' where (binary name like ? or binary author like ?) ';
            $flag = true;
        }

        $flag_category = false;

        if (isset($book_category)) {
            if ($flag) {
                $sql .= ' and ';
            } else {
                $sql .= ' where ';
            }
            $sql .= ' binary category like ? ';

            $flag_category = true;
        }

        // 价格排序
        if (isset($book_order)) {
            if ($book_order == 'desc') {
                $sql .= ' order by price desc';
            } else {
                $sql .= ' order by price';
            }
        }

        $stmt = $db->prepare($sql);

        if ($flag && $flag_category) {
            if (count($book_query) == 1) {
                $stmt->bindValue(1, '%' . trim($book_1) . '%');
                $stmt->bindValue(2, '%' . trim($book_1) . '%');
                $stmt->bindValue(3, '%' . trim($book_category) . '%');
            } else {
                $stmt->bindValue(1, '%' . trim($book_1) . '%');
                $stmt->bindValue(2, '%' . trim($book_2) . '%');
                $stmt->bindValue(3, '%' . trim($book_1) . '%');
                $stmt->bindValue(4, '%' . trim($book_2) . '%');

                $stmt->bindValue(5, '%' . trim($book_category) . '%');
            }
        } else if ($flag) {
            if (count($book_query) == 1) {
                $stmt->bindValue(1, '%' . trim($book_1) . '%');
                $stmt->bindValue(2, '%' . trim($book_1) . '%');
            } else {
                $stmt->bindValue(1, '%' . trim($book_1) . '%');
                $stmt->bindValue(2, '%' . trim($book_2) . '%');
                $stmt->bindValue(3, '%' . trim($book_1) . '%');
                $stmt->bindValue(4, '%' . trim($book_2) . '%');
            }
        } else {
            $stmt->bindValue(1, '%' . trim($book_category) . '%');
        }

    } else {
        $stmt = $db->prepare('select * from book');
    }
}

$stmt->execute();

$row = $stmt->fetchall();

WebResult::JsonResult(array('total' => count($row), 'books' => $row), "ok");